
__all__ = [u'install_qtranslator']

import os
from PyQt4 import (QtCore, QtGui)
from _pylot import MO_FILE_DIR
from pylot.core import (Path, LANG, ENCODING)

__CURRENT_QT_LOCALE = None

QTRANSLATORS = {}

def install_qtranslator(locale):
  global __CURRENT_QT_LOCALE, QTRANSLATORS
  if locale == __CURRENT_QT_LOCALE :
    return
  __CURRENT_QT_LOCALE = locale
  try :
    translator = QTRANSLATORS[locale]
  except KeyError :
    translator = QtCore.QTranslator()
    QTRANSLATORS[locale] = translator

  i18n_path = unicode(Path(MO_FILE_DIR.decode(ENCODING), u'%s.qm' % locale).abspath())
  loaded = translator.load(i18n_path)

  for old_translator in QTRANSLATORS.values():
    QtGui.qApp.instance().removeTranslator(old_translator)

  if loaded :
    QtGui.qApp.instance().installTranslator(translator)
